import numpy
from scipy.io import arff
import pandas as pd
import numpy as np
from sklearn.datasets import load_wine

from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

file_name = 'dataSet/AEEEM/EQ.arff'

data,meta = arff.loadarff(file_name)
# print(meta)
data = pd.DataFrame(data)
wine = load_wine()
x = wine.data
y = wine.target
datasets = data.iloc[:, :-1]
labels = data.iloc[:, -1]
# 将特征数据转为数组
datasets = np.array(datasets)
# print(datasets)
# 标签的转换为0、1
labels = np.array(labels)
for i in range(len(labels)):
    if labels[i] == b'clean':
        labels[i] = numpy.int32(0)
    else:
        labels[i] = numpy.int32(1)

# 训练集和测试集的划分
x_train, x_test, y_train, y_test = train_test_split(datasets, labels, test_size=0.3)
# 转为普通数组
x_train = x_train.tolist()
x_test = x_test.tolist()
y_train = y_train.tolist()
y_test = y_test.tolist()
# print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)
# SVM训练
clf = SVC(kernel='linear', C=1)
clf.fit(x_train,y_train)
y_predict = clf.predict(x_test)
# print(y_predict)
# 计算分类准确率
accuracy = accuracy_score(y_test, y_predict)
print(f"Accuracy: {accuracy}")

